package com.permission.myannotation;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/6/22
 */
@Slf4j
//@EnableAspect
@Component
@Aspect
public class TestAop {

    @Pointcut(value = "@annotation(com.permission.myannotation.TestAnnotation)")
    private void pointcut() {

    }

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        TestAnnotation testAnnotation = method.getAnnotation(TestAnnotation.class);
        if (testAnnotation == null) {
            return;
        }
        log.info("TestAnnotation value:{}", testAnnotation.value());
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object[] objects = joinPoint.getArgs();
        log.info("param:{}", objects);
        try {
            Object object = joinPoint.proceed(objects);
            log.info("doAfter:{}", object);
            return object;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

}
